<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
    // 返回响应最快的那个promise
    function myPromiseRace(promises){
        return new Promise((resolve,reject) => {
            if(!promises || promises.length === 0){
                return
            }
            for(let i = 0; i < promises.length; i++){
                Promise.resolve(promises[i]).then(value => {
                    resolve(value)
                }).catch(err => {
                    reject(err)
                })
            }
        })
    }

    const p1 = new Promise(resolve => setTimeout(resolve, 500, '慢速'));
    const p2 = new Promise(resolve => setTimeout(resolve, 100, '快速'));
    myPromiseRace([p1, p2])
        .then(res => console.log('Test1 应输出"快速":', res));

    // 测试2: 第一个失败Promise触发结果
    const p3 = new Promise((_, reject) => setTimeout(reject, 100, '错误优先'));
    const p4 = new Promise(resolve => setTimeout(resolve, 500));
    myPromiseRace([p3, p4])
        .catch(err => console.log('Test2 应输出"错误优先":', err));

    // 测试3: 非Promise值测试
    myPromiseRace([123, Promise.resolve('hello')])
        .then(res => console.log('Test3 应输出123:', res));

    // 测试4: 空数组（Promise永远pending）
    myPromiseRace([])
        .then(() => console.log('永远不会执行'))
        .catch(() => console.log('永远不会执行'));
</script>
</html>